/**
 * @file    hal_uart.h
 * @author  Azolla (1228449928@qq.com)
 * @brief   led contorl PWM
 * @version 0.1
 * @date    2023-08-01
 * 
 * @copyright Copyright (c) 2023
 * */
#ifndef __HAL_UART__
#define __HAL_UART__

#include "hal_config.h"
#include "driver/uart.h"

typedef struct {
    int txd_pin;
    int rxd_pin;
    int rts_pin;
    int cts_pin;
    uint16_t buff_size;
    uart_port_t   port;
    uart_mode_t   mode;
    uart_config_t config;
} hal_uart_config_t;

#define HAL_UART_CONFIG_DEFAULT(UART_PIN_TXD, UART_PIN_RXD, UART_PORT, BAUD_RATE) { \
    .txd_pin = UART_PIN_TXD,  				        \
    .rxd_pin = UART_PIN_RXD, 	 				    \
    .rts_pin = UART_PIN_NO_CHANGE,  				\
    .cts_pin = UART_PIN_NO_CHANGE,  				\
    .port = UART_PORT,								\
    .mode = UART_MODE_UART,							\
    .config.baud_rate = BAUD_RATE,					\
    .config.data_bits = UART_DATA_8_BITS,			\
    .config.parity    = UART_PARITY_DISABLE,		\
    .config.stop_bits = UART_STOP_BITS_1,			\
    .config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,	\
    .config.source_clk= UART_SCLK_APB,				\
    .config.rx_flow_ctrl_thresh = 64,				\
    .buff_size = 128,								\
}

typedef void (*uart_recv_callback_t)(uart_port_t port, uint8_t *data, uint16_t len);

void hal_uart_recv_register_callback(uart_port_t port, uint16_t buff_size, uart_recv_callback_t cb_func);

void hal_uart_init(uart_recv_callback_t cb_func);

void hal_uart_recv_callback(uart_port_t port, uint8_t *data, uint16_t len);

void hal_uart_driver_install(hal_uart_config_t uart_cfg);
 
void hal_uart_write(const uart_port_t port, const uint8_t *data, uint16_t len);

#endif /* __HAL_UART__ END. */

